package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.model.db.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 问题服务接口实现类
 */
@DubboService
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 根据当前用户id查询tb_question表得到问题内容
     *
     * @param userId
     * @return
     */
    @Override
    public Question findQuestionById(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return questionMapper.selectOne(queryWrapper);
    }

    /**
     * 设置陌生人问题 - 保存
     *
     * @param question
     */
    @Override
    public void add(Question question) {
        //1.查询tb_question表记录是否存在
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", question.getUserId());
        Question qs = questionMapper.selectOne(queryWrapper);
        //2.不存在 保存
        if (qs == null) {
            questionMapper.insert(question);
        }else {
            //3.存在 更新
            question.setId(qs.getId());//将数据库查询的主键id 设置到question对象中
            questionMapper.updateById(question);
        }
    }
}
